var http = require('http');


// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  
  if ( request.method === 'POST' ) {

     // the body of the POST is JSON payload.
     var data = '';
     request.on('data', function(chunk) {
                          data += chunk;
                                     });

     request.on('end', function() {
                                                           try 
                                                           {
                                                        	   data = JSON.parse(data);
                                                               console.log(data);
                                                               var objToJson = createJSONBack(data.userID); 
                                                               writeResponse(response, objToJson);
                                                           } 
                                                           catch (e) 
                                                           {
                                                               console.log(e);
                                                           } 

                                                     }
     );
  }
});

function createJSONBack(userID){
	var date = new Date();
	
	var dd = date.getDate().toString();
	var mm = (date.getMonth() + 1);
	var yy = date.getFullYear().toString();
	if ( mm < 10)
		{
		mm = "0" + mm.toString();
		}
	else
		{
		mm = mm.toString();
		}
	
	var today = dd + mm + yy;
	date.setDate(date.getDate() -1);
	var dd2 = date.getDate().toString();
	var mm2 = (date.getMonth() + 1);
	var yy2 = date.getFullYear().toString();
	if ( mm2 < 10)
	{
	mm2 = "0" + mm2.toString();
	}
	else
	{
	mm2 = mm2.toString();
	}
	var yesterday = dd2 + mm2 + yy2;
	
	if(userID==="11"){
		var jsonBack = {status:200, id:userID,dateToday:today, dateYesterday:yesterday, wbsArrayToday:[{wbs:"T1232323"}, {wbs:"T1232324"}, {wbs:"T1232325"}], wbsArrayYesterday:[{wbs:"Y1232333"}, {wbs:"Y1232334"}, {wbs:"Y1232335"}]};
		return jsonBack;
	}
	else if(userID==="22"){
		var jsonBack = {status:200, id:userID,dateToday:today, dateYesterday:yesterday, wbsArrayToday:[{wbs:"T2232323"}, {wbs:"T2232324"}, {wbs:"T2232325"}], wbsArrayYesterday:[{wbs:"Y2232333"}, {wbs:"Y2232334"}, {wbs:"Y2232335"}]};
		return jsonBack;
	}
	else if(userID==="33"){
		var jsonBack = {status:200, id:userID,dateToday:today, dateYesterday:yesterday, wbsArrayToday:[{wbs:"T3232323"}, {wbs:"T3232324"}, {wbs:"T3232325"}], wbsArrayYesterday:[{wbs:"Y3232333"}, {wbs:"Y3232334"}, {wbs:"Y3232335"}]};
		return jsonBack;
	}
	else{
		var jsonBack = {status:1};
		return jsonBack;
	}
}

function writeResponse(response, jsonObj){
            response.writeHeader(200, {"Content-Type": "Application/json"});
            response.write(JSON.stringify(jsonObj));
            response.end();
}

// Listen on port 8000, IP default
server.listen(8888);

console.log("Server running...");



